#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - B2C微支付服务
"""

from jlpay.services.base_service import BaseService
from jlpay.model.trans.b2c.micro_pay import (
    MicroPayRequest, MicroPayResponse,
    MicroPayPreAuthRequest, MicroPayPreAuthResponse
)


class MicroPayService(BaseService):
    """微支付服务"""
    
    def __init__(self, config):
        """
        初始化微支付服务
        
        Args:
            config: OrgConfig实例
        """
        super().__init__(config)
    
    def get_api_url(self, request_class, api_path=None):
        """
        获取API URL
        
        Args:
            request_class: 请求类
            api_path: 接口路径
            
        Returns:
            完整的API URL
        """
        base_url = self.config.url
        
        if api_path:
            return f"{base_url}{api_path}"
        
        # 直接使用Request类中定义的完整API路径
        api_url = getattr(request_class, 'API_URL', '')
        
        if not api_url:
            # 如果没有定义API_URL，使用默认路径
            return f"{base_url}/open/trans"
        
        # 直接返回完整路径
        return f"{base_url}{api_url}"
    
    def micro_pay(self, request: MicroPayRequest) -> MicroPayResponse:
        """
        微支付
        
        Args:
            request: MicroPayRequest实例
            
        Returns:
            MicroPayResponse: 微支付响应结果
        """
        return self.post(request, MicroPayResponse)
    
    def micro_pay_pre_auth(self, request: MicroPayPreAuthRequest) -> MicroPayPreAuthResponse:
        """
        微支付预授权
        
        Args:
            request: MicroPayPreAuthRequest实例
            
        Returns:
            MicroPayPreAuthResponse: 微支付预授权响应结果
        """
        return self.post(request, MicroPayPreAuthResponse) 